#pragma once 

#include <iostream>
#include <string>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Util.hpp"

const string sep = "\r\n";
const string default_root = "./wwwroot";
const string home_page = "index.html";
const string html_404 = "wwwroot/404.html";

using namespace std;

class HttpRequest
{
public:
    HttpRequest(){}
    ~HttpRequest(){}
    void parse()
    {
        // 1.从请求里拿到第一行，分隔符\r\n
        string line = Util::GetOnLine(inbuffer,sep);
        if(line == "") 
            return ;
        cout << "line: " << line << endl;
        // 2.获取到三个字段
        stringstream ss(line);
        ss >> method >> url >> httpversion;

        // 3. 添加web默认路径
        path = default_root;    //./wwwroot
        path += url;            //./wwwroot/a/b/c.html
        if(path[path.size()-1] == '/')
            path += home_page;

        // 4. 获取path对应的资源后缀
        auto pos = path.rfind(".");
        if(pos == string::npos) { suffix = ".html";}
        else {suffix = path.substr(pos);}
        // 5. 标定资源的大小
        struct stat st;
        int n = stat(path.c_str(), &st);
        if(n == 0) { size = st.st_size; }
        else { size = -1; }
    }
public:
    string inbuffer;

    string method;
    string url;
    string httpversion;
    string path;
    string suffix;
    int size;
};


class HttpResponse
{
public:
    string outbuffer;
};